package com.administrators.bag;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;



/**
 * Servlet implementation class ZCServlet
 */
@WebServlet("/ZCServlet")
public class ZCServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ZCServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		//
		String x=request.getParameter("zhanghao");
		String y=request.getParameter("mima");
		String z=request.getParameter("mima1");
		//查重
		String sq = "select * from wzs_administrators where `name`='"+x+"'";
		System.out.println(sq);
		QueryDataResult qdr = DBManager.executeQuery(sq);
		Administrators emp=null;
		try {
			while(qdr.rs.next()) {
				emp=new Administrators(qdr.rs.getString(1),qdr.rs.getString(2),qdr.rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		if(emp!=null) {
			System.out.println("以存在");
		}else {
			if(y.equals(z)) {
				String sql = String.format("insert into wzs_administrators(name,password) values ('%s','%s')", x,z);
				int i = DBManager.executeUpdate(sql);
				System.out.println(i==1?"成功":"失败");
				if(i==1) {
					request.getRequestDispatcher("SuccessServlet").forward(request, response);
				}
			}else {
				System.out.println("密码不一致！");
				out.print("密码不一致");
			}
		}
		
		
		
		
	}
	

}
